/*
 * Copyright 2007 Xplayat, S.L. All rights reserved.
 * Propiedad de Xplayat. 
 */
package es.xpt.ysf.view.menu.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import es.xpt.ysf.logging.LogManager;
import es.xpt.ysf.view.model.LocaleDescriptionBean;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="option")
public class OptionBean {

	private String id;
	private HashMap<String, LocaleDescriptionBean> descriptions = new HashMap<String, LocaleDescriptionBean>(1);
	
	@XmlElement(name="option")
	private List<OptionBean> children = new ArrayList<OptionBean>();

	@XmlAttribute
	private String disabled;
	@XmlAttribute
	private String url;
	@XmlAttribute
	private String action;
	@XmlAttribute
	private String rendered;
	@XmlAttribute
	private String grant;
	@XmlAttribute
	private String target;
	@XmlAttribute
	private String onclick;
	@XmlAttribute
	private String onkeydown;

	
	public OptionBean() {
	}
	
	public String executeAction() { // NO_UCD
		try {
			FacesContext fc = FacesContext.getCurrentInstance();
			return (String)fc.getApplication().getExpressionFactory()
			.createMethodExpression(fc.getELContext(), action, null, new Class[0])
			.invoke(fc.getELContext(), new Object[0]);
		} catch (Exception e) {
			LogManager.getLog(getClass()).error("OptionBean class is designed to be used with a JSF Context", e);
			return null;
		}
	}
	
	public Boolean getRenderedEval() {
		if (rendered==null || rendered.trim().length() == 0)
			return true;
		try {
			FacesContext fc = FacesContext.getCurrentInstance();
			return (Boolean)fc.getApplication().getExpressionFactory()
			.createValueExpression(fc.getELContext(), rendered, Boolean.class)
			.getValue(fc.getELContext());
		} catch (Exception e) {
			LogManager.getLog(getClass()).error("OptionBean class is designed to be used with a JSF Context", e);
			return true;
		}
	}
	
	public String getOnclickEval() {
		if (onclick!=null) {			
			try {
				FacesContext fc = FacesContext.getCurrentInstance();
				String result = (String)fc.getApplication().getExpressionFactory()
				.createValueExpression(fc.getELContext(), onclick, String.class).getValue(fc.getELContext());
				return result;
			} catch (Exception e) {
				LogManager.getLog(getClass()).error("OptionBean class is designed to be used with a JSF Context", e);				
			}
		}
		return onclick;
	}
	
	public String getOnkeydownEval() {
		if (onkeydown!=null) {			
			try {
				FacesContext fc = FacesContext.getCurrentInstance();
				String result = (String)fc.getApplication().getExpressionFactory()
				.createValueExpression(fc.getELContext(), onkeydown, String.class).getValue(fc.getELContext());
				return result;
			} catch (Exception e) {
				LogManager.getLog(getClass()).error("OptionBean class is designed to be used with a JSF Context", e);				
			}
		}
		return onkeydown;
	}
	
	public Boolean getDisabledEval() {
		if (disabled==null || disabled.trim().length() == 0)
			return false;
		try {
			
			FacesContext fc = FacesContext.getCurrentInstance();
			return (Boolean)fc.getApplication().getExpressionFactory()
			.createValueExpression(fc.getELContext(), disabled, Boolean.class)
			.getValue(fc.getELContext());
		} catch (Exception e) {
			LogManager.getLog(getClass()).error("OptionBean class is designed to be used with a JSF Context", e);
			return false;
		}
	}

	public String getTargetEval() {
		return (target!=null)?target:"_top";
	}
	
	public String getUrl() {
		if ( url!=null ) {
			try {
				FacesContext fc = FacesContext.getCurrentInstance();
				String result = (String)fc.getApplication().getExpressionFactory()
				.createValueExpression(fc.getELContext(), url, String.class).getValue(fc.getELContext());
				return result;
			} catch (Exception e) {
				LogManager.getLog(getClass()).error("OptionBean class is designed to be used with a JSF Context", e);
			}
		}
		return url;
	}

	public void setOptions(List<OptionBean> options) {
		setChildren(options);
	}

	public List<OptionBean> getOptions() {
		return getChildren();
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setGrant(String grant) {
		this.grant = grant;
	}

	public String getGrant() {
		return grant;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}
	
	public void setTarget(String target) {
		this.target = target;
	}

	public String isDisabled() { // NO_UCD
		return disabled;
	}

	/**
	 * @param action the action to set
	 */
	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * @return the action
	 */
	public String getAction() {
		return action;
	}

	/**
	 * @return the rendered
	 */
	public String getRendered() {
		return rendered;
	}

	/**
	 * @param rendered the rendered to set
	 */
	public void setRendered(String rendered) {
		this.rendered = rendered;
	}
	
	/**
	 * @return the onclick
	 */
	public String getOnclick() {
		return onclick;
	}

	/**
	 * @param onclick the onclick to set
	 */
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	/**
	 * @return the onkeydown
	 */
	public String getOnkeydown() {
		return onkeydown;
	}

	/**
	 * @param onkeydown the onkeydown to set
	 */
	public void setOnkeydown(String onkeydown) {
		this.onkeydown = onkeydown;
	}
	
	public String getDisabled() {
		return disabled;
	}

	public String getTarget() {
		return target;
	}
		
	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * @return the descriptions
	 */
	public HashMap<String, LocaleDescriptionBean> getDescriptions() {
		return descriptions;
	}
	/**
	 * @param descriptions the descriptions to set
	 */
	public void setDescriptions(
			HashMap<String, LocaleDescriptionBean> descriptions) {
		this.descriptions = descriptions;
	}

	/**
	 * @return the children
	 */
	private List<OptionBean> getChildren() {
		return children;
	}

	/**
	 * @param children the children to set
	 */
	private void setChildren(List<OptionBean> children) {
		this.children = children;
	}

}
